package com.yuer.image;

import com.yuer.ProcessHandler;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import us.codecraft.webmagic.Page;

import java.util.List;

/**
 * 获取章节
 */
public class ImageProcessHandler implements ProcessHandler {

    @Override
    public void process(Page page) {

        String url = page.getHtml().$("#bookImg").css("img", "src").toString();
        System.out.println("========== " + url);

        if (StringUtils.isNotBlank(url)) {

            String bookName = page.getHtml().$(".book-info").$("h1").$("em").toString();
            bookName = Jsoup.parse(bookName).text();

            if (StringUtils.isNotBlank(bookName)) {
                url = "https:" + url;

                bookName = bookName + ".jpeg";

                System.out.println(">>>>>> " + bookName + ", " + url);
                page.putField(bookName, url);
            }

        } else {
            // 非内容页就是导航页
            List<String> links = page.getHtml().links().all();

            page.addTargetRequests(links);
        }
    }
}
